package com.kld.simplelauncher.app;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kld.simplelauncher.R;
import com.kld.simplelauncher.utils.AppUtils;
import com.kld.simplelauncher.utils.EntryActivity;

import java.util.List;

public class ShowAppFragment extends Fragment implements AllAppAdapter.OnAppClickListener {
    private static final String TAG = "ShowAppFragment";
    private int currPage = 0;
    private List<EntryActivity> packageInfoList;
    private RecyclerView showAppData;
    private AllAppAdapter adapter;
    public ShowAppFragment(int page,List<EntryActivity> packageInfoList) {
        this.currPage = page;
        this.packageInfoList = packageInfoList;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_show_app_layout,container,false);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        showAppData = view.findViewById(R.id.show_app_data);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 5, GridLayoutManager.VERTICAL, false);
        showAppData.setLayoutManager(gridLayoutManager);
        showAppData.setItemAnimator(new DefaultItemAnimator());
        adapter = new AllAppAdapter(getActivity(),packageInfoList);
        adapter.addListener(this);
        showAppData.setAdapter(adapter);
    }

    public void setAppList(List<EntryActivity> packageInfoList) {
        this.packageInfoList = packageInfoList;
        adapter.setAppList(packageInfoList);
    }

    @Override
    public void onAppClickListener(View view, int position, EntryActivity info) {
        Log.d(TAG, "onAppClickListener: displayName = " + info.displayName + "  packageName = " + info.packageName + "   activityName = " + info.activityName);
        AppUtils.doRunActivity(getActivity(),info.packageName,info.activityName);
    }

    @Override
    public void onAppLongClickListener(View view, int position, EntryActivity info) {
        Log.d(TAG, "onAppLongClickListener: info = " + info.displayName);
            AppUtils.unInstallApk(getActivity(),info.packageName);
    }

}
